function TableList()
{
    this.id = "tl";
    this.url = "";
    this.page = 1;
    this.pageSize = 10;
    this.sort = "";
    this.sortOrder = "desc";
    this.callback = null;
    this.checkbox = false;

    //列
    this.fields = [];
    this.columnsMap = null;
    this.searchForm = "";
    this.tableDiv = "";

    this.isInit = false;
    this.rows = [];

    this.selected = [];
    this.rowData = [];

    this.init = function (options)
    {
        var g = this;
        if (g.isInit)
        {
            return;
        }

        options = options || {};

        g.url = options.url ? options.url : "";
        g.page = options.page ? options.page : 1;
        g.pageSize = options.pageSize ? options.pageSize : 10;
        g.sort = options.sort ? options.sort : "";
        g.sortOrder = options.sortOrder ? options.sortOrder : "desc";
        g.callback = options.callback ? options.callback : null;
        g.searchForm = options.searchForm ? options.searchForm : "";
        g.tableDiv = options.tableDiv ? options.tableDiv : "";
        g.fields = options.fields ? options.fields : [];
        g.checkbox = options.checkbox ? true : false;

        if (g.fields && g.fields.length > 0)
        {
            g.columnsMap = {};
            for (var index in g.fields)
            {
                var field = g.fields[index];
                g.columnsMap[field.name] = field;
            }
        }

        $(g.searchForm).find("button").first().bind("click", function ()
        {
            g.ajax();
        });

        g.ajax();
    };

    this._getRowDomId = function (index)
    {
        var g = this;
        return g.id + "-" + index;
    };

    //生成表格与分页
    this._render = function (result)
    {
        var g = this;
        if (!result.data)
        {
            return;
        }

        var queryResult = result.data;
        var list = queryResult.data;
        var html = [];

        html.push("<table class=\"table table-striped table-bordered table-hover\">");

        //表头
        html.push("<thead>");
        html.push("<tr>");

        if (g.checkbox)
        {
            html.push("<th style=\"width:50px\">");
            html.push("<input type=\"checkbox\" id=\"tl-checkAll\">");
            html.push("</th>")
        }

        for (var fIndex in g.fields)
        {
            var col = g.fields[fIndex];
            html.push("<th style=\"width:");
            html.push((col.width ? col.width : 120));
            html.push("px\">");

            html.push(col.display);
            html.push("</th>");
        }
        html.push("</tr>");
        html.push("</thead>");

        //表身
        html.push("<tbody>");

        if (list.length == 0)
        {
            html.push("<tr>");
            html.push("<td colspan=\"" + (g.fields.length + (g.checkbox ? 1 : 0)) + "\">暂无数据</td>");
            html.push("</tr>");
        }
        else
        {
            g.selected = [];
            g.rowData = list;
            for (var rIndex in list)
            {
                var row = list[rIndex];
                g[rIndex] = row;
                var domId = g._getRowDomId(rIndex);
                html.push("<tr id=\"" + domId + "\">");

                if (g.checkbox)
                {
                    html.push("<td>");
                    html.push("<input type=\"checkbox\" >");
                    html.push("</td>");
                }

                for (var index in g.fields)
                {
                    var field = g.fields[index];
                    html.push("<td>");

                    var val = row[field.name];
                    if (field.render)
                    {
                        val = field.render.call(g, [
                            val,
                            row
                        ]);
                    }
                    else if (field.type)
                    {
                        val = g._getRenderedData(field.type, val, field);
                    }

                    html.push(val);
                    html.push("</td>");
                }
                html.push("</tr>");
            }
        }

        html.push("</tbody>");
        html.push("<table>");

        //分页
        var page = queryResult.page;
        var pageCount = queryResult.pageCount;

        var pageStart = Math.max(page - 5, 1);
        var pageEnd = Math.min(page + 5, pageCount);

        html.push("<div class=\"pagination-right pull-right\">");
        html.push("    <ul class=\"pagination\">");

        if (page == 1)
        {
            html.push("        <li class=\"disabled\"><a href=\"javascript:void(0)\" data-page=\"1\">&#8249;&#8249;</a></li>");
            html.push("        <li class=\"disabled\"><a href=\"javascript:void(0)\" data-page=\"" + (Math.max(page - 1, 1)) + "\">&#8249;</a></li>");
        }
        else
        {
            html.push("        <li><a href=\"javascript:void(0)\" data-page=\"1\">&#8249;&#8249;</a></li>");
            html.push("        <li><a href=\"javascript:void(0)\" data-page=\"" + (Math.max(page - 1, 1)) + "\">&#8249;</a></li>");
        }

        for (var i = pageStart; i <= pageEnd; ++i)
        {
            html.push("        <li class=" + (page == i ? "active" : "") + "><a href=\"javascript:void(0)\" data-page=\"" + i + "\">" + i + "</a></li>");
        }

        if (page == pageEnd)
        {
            html.push("        <li class=\"disabled\"><a href=\"javascript:void(0)\" data-page=\"" + (Math.min(page + 1, pageCount)) + "\">&#8250;</a></li>");
            html.push("        <li class=\"disabled\"><a href=\"javascript:void(0)\" data-page=\"" + (pageCount) + "\">&#8250;&#8250;</a></li>");
        }
        else
        {
            html.push("        <li><a href=\"javascript:void(0)\" data-page=\"" + (Math.min(page + 1, pageCount)) + "\">&#8250;</a></li>");
            html.push("        <li><a href=\"javascript:void(0)\" data-page=\"" + (pageCount) + "\">&#8250;&#8250;</a></li>");
        }
        html.push("    </ul>");
        html.push("</div>");

        return html.join("");
    };

    //生成查询参数
    this._buidParams = function ()
    {
        var params = {};
        var g = this;
        var where = [];
        $(g.searchForm).find("input").each(function (index, obj)
        {
            var o = $(obj);
            var val = o.val();
            if (!val || "" == $.trim(val))
            {
                return;
            }

            var condition = {};
            condition.field = o.data("field");
            condition.op = o.data("op").toUpperCase();
            condition.val = val;
            where.push(condition);
        });

        //查询条件
        if (where.length > 0)
        {
            params.where = JSON.stringify(where);
        }

        params.page = g.page;
        params.pageSize = g.pageSize;

        return params;
    };

    this._select = function (rowId)
    {
        var g = this;
        var domId = g._getRowDomId(rowId);
        var dom = $("#" + domId);

        var row = g._getRowData(rowId);
        row._id = rowId;

        if (g.selected.length > 0)
        {
            var unselected = g._unselect(g.selected[0]._id);
            if (unselected && (unselected._id == rowId))
            {
                return;
            }
        }

        dom.addClass("info");

        g.selected.push(row);
    };

    this._unselect = function (rowId)
    {
        var g = this;
        var domId = g._getRowDomId(rowId);
        var dom = $("#" + domId);
        dom.removeClass("info");
        return g.selected.pop();
    };

    this._getRowData = function (rowId)
    {
        var g = this;
        return g.rowData[rowId];
    };

    this._bindEvent = function ()
    {
        var g = this;
        $(g.tableDiv).find("ul.pagination  a").bind("click", function ()
        {
            var a = $(this);
            g.page = a.data("page");
            g.ajax();
        });

        $(g.tableDiv).find("tbody").bind("click.tbody", function (e)
        {
            var row = g._getSrcElementByEvent(e);
            var domId = row.attr("id");
            var rowId = domId.split("-")[1];
            g._select(rowId);
        });
    };

    this._getSrcElementByEvent = function (e)
    {
        var obj = (e.target || e.srcElement);
        return $(obj).parent();
    };

    this.ajax = function ()
    {
        var g = this;
        var params = g._buidParams();
        $.ajax(g.url, {
            data : params,
            dataType : 'json',
            type : 'post',
            success : function (data, isError, message)
            {
                $(g.tableDiv).html(g._render(data));
                g._bindEvent();

                if (g.callback)
                {
                    g.callback(html);
                }
            },
            error : function ()
            {
                console.log(arguments);
            }

        });
    };

    this.getSelected = function ()
    {
        var g = this;
        return g.selected.length > 0 ? g.selected[0] : null;
    };

    this._getRenderedData = function (type, val, field)
    {

        var fmt;
        switch(type)
        {
            case 'date':
                fmt = field.format ? field.format : TableList.defaults.format;
                return common.format(val, fmt);
            default :
                break;
        }
    };
}

TableList.defaults = {
    format : "yyyy-MM-dd hh:mm:ss"
};

TableList.defaults.field = {
    display : null,
    name : null,
    isSort : false,
    type : null,
    render : null
};